package com.wx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.*;
import com.wx.entity.SysBanner;
import com.wx.holder.SecurityUserHolder;
import com.wx.service.SysBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>Title:	  weixin-go-with-you <br/> </p>
 * <p>Description TODO <br/> </p>
 * <p>Company:    医百科技有限公司  <br/> </p>
 *
 * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>  <br/>
 * @Date 2021/4/5 21:26
 */
@RestController
@RequestMapping(value = "/banner", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "轮播图管理", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SysBannerController {

    @Resource
    private SysBannerService sysBannerService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    @PostMapping(value = "/listPage")
    @ApiOperation(httpMethod = "POST", value = "分页查询轮播图")
    public Wrapper<PageInfo<SysBanner>> listPage(@ApiParam(name = "banner", value = "轮播图信息") @RequestBody PageParam<SysBanner> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<SysBanner> listPage = sysBannerService.listPage(pageParam.getParam());
        PageInfo<SysBanner> pageInfo = new PageInfo<>(listPage);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增")
    public Wrapper<Integer> save(@ApiParam(name = "banner", value = "轮播图") @RequestBody @Validated(CreateValidate.class) SysBanner sysBanner) {
        SecurityUser securityUser = securityUserHolder.getUser();
        sysBanner.setCreateBy(securityUser.getUserId());
        sysBanner.setCreator(securityUser.getRealName());
        sysBanner.setUpdateBy(securityUser.getUserId());
        sysBanner.setUpdater(securityUser.getRealName());
        int result = sysBannerService.saveBanner(sysBanner);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping(value = "/delete/{id}")
    @ApiOperation(httpMethod = "POST", value = "删除")
    public Wrapper<Integer> delete(@PathVariable("id") String id) {
        SecurityUser securityUser = securityUserHolder.getUser();
        int result = sysBannerService.deleteById(id,securityUser.getUserId(),securityUser.getRealName());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping(value = "/update")
    @ApiOperation(httpMethod = "POST", value = "更新")
    public Wrapper<Integer> update(@ApiParam(name = "banner", value = "轮播图") @RequestBody @Validated SysBanner sysBanner) {
        SecurityUser securityUser = securityUserHolder.getUser();
        sysBanner.setUpdateBy(securityUser.getUserId());
        sysBanner.setUpdater(securityUser.getRealName());
        sysBanner.setUpdateDate(new Date());
        int result = sysBannerService.updateBanner(sysBanner);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping(value = "/updateSatus/{id}/{status}")
    @ApiOperation(httpMethod = "POST", value = "禁用/启用")
    public Wrapper<Integer> updateSatus(@PathVariable("id") String id , @PathVariable("status") Integer status) {
        SecurityUser securityUser = securityUserHolder.getUser();
        SysBanner update = new SysBanner();
        update.setUpdateBy(securityUser.getUserId());
        update.setUpdater(securityUser.getRealName());
        update.setUpdateDate(new Date());
        update.setId(id);
        update.setStatus(status);
        int result = sysBannerService.update(update);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }
}
